<?php
namespace Modules\Activity\Http\Controllers\Client\RotateLottery;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\RotateLottery\Activity;
use \Modules\Activity\Entities\ActivityOrder as Order;

use Validator;
use ThrowException;
use Auth;
use DB;
use Cache;


// 福袋订单
class OrderController extends Controller
{
    // 创建订单
    public function run(Request $req, $uuid) 
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        // $cached = Cache::get($req->page_uuid) ?: ThrowException::Conflict('页面已失效，请刷新后重试~');

        // 扭蛋机
        $activity = Activity::getEntity([
            'uuid' => $uuid
        ]);

        // 购买总数
        $total = 1;

        // 检查是否超出限售
        $activity->checkSkuStock($total) || ThrowException::Conflict('库存不足~');

        // 检查次数限制
        $activity->checkPerUserLimit($user, $total);

        DB::beginTransaction();

        // 运行抽奖逻辑
        $package = $activity->run($user, $total);

        DB::commit();


        $skus = $package->skus;

        $resList = [];
        foreach ($skus as $packageSku) {
            array_push($resList, [
                'id' => $packageSku->id,
                'uuid' => $packageSku->uuid,
                'title' => $packageSku->title,
                'thumb' => $packageSku->thumb,
                'total' => $packageSku->total,
                'sku_type' => $packageSku->sku_type,
                'base_sku_id' => $packageSku->sku_id
            ]);
        }

        // 返回订单内容
        return [
            'skus' => $resList
        ];
    }

    // 试一试
    public function try(Request $req) 
    {
        $rule = [
            'activity_id' => ['required'],
            'total' =>  ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $user = Auth::requireLoginUser();

        // $cached = Cache::get($req->page_uuid) ?: ThrowException::Conflict('页面已失效，请刷新后重试~');

        // 福袋
        $activity = Activity::getEntity([
            'id' => $req->activity_id
        ]);

        // 购买总数
        $total = $req->total;

        // 仅仅
        DB::beginTransaction();

        // 取出sku
        $skuMaps = $activity->pickLuckySkusForTry($total, $user, null);

        $list = [];

        // 此处不要提交到数据库保存
        foreach ($skuMaps as $skuMap) {
            $sku = $skuMap['skuable'];
            array_push($list, [
                "title" => $sku->title,
                "number" => $sku->number,
                "thumb" => $sku->thumb,
                "money_price" => $sku->money_price,       // 价值 (人民币)
                "total" => $skuMap['total'],                 // 中奖数量
            ]);
        }

        $uuid = 'try_' . uniqid();

        $cached = [
            'sku_type' => 'egg_lottery_sku',
            'sku_id' => $skuMaps[0]['skuable']->id,
            'skus' => $list
        ];
        Cache::put($uuid, $cached, 1800);

        return [
            'package_uuid' => $uuid,
            // 'skus' => $list
        ];

    }
}
